﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;

namespace DomainModel.Concrete
{
    // Implementation of the Questions section of the database.
    // The middle layer of the DAL.
    public class SqlQuestionsRepository : IQuestionsRepository
    {
        private Table<Question> questionsTable;

        public SqlQuestionsRepository(string connectionString)
        {
            questionsTable = (new DataContext(connectionString)).GetTable<Question>();
        }

        public IQueryable<Question> Questions
        {
            get { return questionsTable; }
        }

        public void SaveQuestion(Question question)
        {
            if (question.QuestionID == 0)
            {
                questionsTable.InsertOnSubmit(question);
            }
            else
            {
                questionsTable.Attach(question);
                questionsTable.Context.Refresh(RefreshMode.KeepCurrentValues, question);
            }

            questionsTable.Context.SubmitChanges();
        }

        public void DeleteQuestion(Question question)
        {
            questionsTable.DeleteOnSubmit(question);
            questionsTable.Context.SubmitChanges();
        }
    }
}
